package com.fire.eyes.controller;

import com.alibaba.fastjson.JSON;
import com.fire.core.mode.TMitUserToken;
import com.fire.core.service.RedisService;
import com.fire.core.util.AjaxResult;
import com.fire.eyes.constants.ErrorCodeConstants;
import com.fire.eyes.model.FireDangrousResouceModel;
import com.fire.eyes.model.FireSupervisorUserModel;
import com.fire.eyes.response.DresDTO;
import com.fire.eyes.service.IFireDangrousResouceService;
import com.fire.eyes.service.IFireSupervisorUserService;
import com.fire.eyes.validation.TokenDTO;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
public class DresController {
	private final static Logger logger = Logger.getLogger(DresController.class);

	@Autowired
	private RedisService redisService;
	@Autowired
	private IFireSupervisorUserService fireSupervisorUserService;
	@Autowired
	private IFireDangrousResouceService fireDangrousResouceService;

	/**
	 * @return
	 * @author zengzhengfu
	 */
	@PostMapping(value = "/mydresouces")
	@ResponseBody
	public AjaxResult mydresouces(@RequestBody @Validated TokenDTO tokenDTO, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			for (FieldError fieldError : bindingResult.getFieldErrors()) {
				String message = fieldError.getDefaultMessage();
				logger.error(message);
				return new AjaxResult(ErrorCodeConstants.PARAM_WRONGFULL, "参数有误," + message, "参数有误," + message);
			}
		}
		String token = tokenDTO.getToken();
		TMitUserToken userToken = (TMitUserToken) redisService.get(token);
		FireSupervisorUserModel supervisorUserModel = fireSupervisorUserService.selectById(userToken.getUserid());

		//List<DresDTO> list = fireDangrousResouceService.selectListBySupervisorid("sv10002");
		List<DresDTO> list = fireDangrousResouceService.selectListBySupervisorid(supervisorUserModel.getSupervisor(),tokenDTO.getPage(),tokenDTO.getPagesize());

		return new AjaxResult(ErrorCodeConstants.SUCCESS, "获取成功", list, "获取成功");
	}

	/**
	 * @return
	 * @author zengzhengfu
	 */
	@PostMapping(value = "/dresoucetails")
	@ResponseBody
	public AjaxResult dresoucetails(@RequestBody Map json) {
		Object obj = json.get("dresourceid");
		if (obj == null) {
			return new AjaxResult(ErrorCodeConstants.PARAM_WRONGFULL, "参数不能为空", "参数不能为空");
		}
		String dresourceid = (String) obj;
		FireDangrousResouceModel fireDangrousResouceModel = fireDangrousResouceService.selectById(dresourceid);
		return new AjaxResult(ErrorCodeConstants.SUCCESS, "获取成功", fireDangrousResouceModel, "获取成功");
	}
}
